<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_GenericDashboardAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$moduleName = $this->getRequestParameter("configModule");

		$parentDoc = $this->getDocumentProvider()->getByUniqueId($this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));

        if($parentDoc->getModel() == 'modules_website/website')
        {
            $method = 'get'.ucfirst($moduleName).'Folder';
            $countFolders = count($method);

            $params = $this->getXmlConfig($moduleName, $parentDoc);

            $documents = $this->getDocumentProvider()   ->loadDocuments($params["docType"])
                                    ->addDescOrder("document_modificationdate")
                                    ->whereIsEqual('websiteId', $parentDoc->getUniqueId())
                                    ->limit(10)
                                    ->retrieveDocuments($this->getRequestParameter("docLang"));
        }
        else
        {
            $countFolders = count($parentDoc->getFolder());
            $params = $this->getXmlConfig($moduleName, $parentDoc);

            $documents = $this->getDocumentProvider()   ->loadDocuments($params["docType"])
                                    ->addDescOrder("document_modificationdate")
                                    ->limit(10)
                                    ->retrieveDocuments($this->getRequestParameter("docLang"));
        }

        $this->addToModel("moduleName", $moduleName);
		$this->addToModel("countFolders", $countFolders);
		$this->addToModel("documents", $documents);
		$this->addToModel("folder", $parentDoc);
		$this->addToModel("params", $params);
		$this->setView('success');
	}	
	
	public function validate()
	{
		return true;
	}

	private function getXmlFilePath($moduleName, $fileName)
	{
		if(!isset($this->xmlPath[$moduleName][$fileName]) || is_null($this->xmlPath[$moduleName][$fileName]))
		{
		    $path = DIRECTORY_SEPARATOR.$moduleName.DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR.$fileName;

		    if(is_file(WEBAPP_MODULES_DIR.$path))
		    {
			    $this->xmlPath[$moduleName][$fileName] = WEBAPP_MODULES_DIR.$path;
		    }
		    if(is_file(MODULES_DIR.$path))
		    {
			    $this->xmlPath[$moduleName][$fileName] = MODULES_DIR.$path;
		    }

		    if(is_null($this->xmlPath[$moduleName][$fileName]))
		    {
			throw new Exception("File folderList.xml does not exist in module ".$moduleName);
		    }
		}

		return $this->xmlPath[$moduleName][$fileName];
	}

	private function getXmlConfig($moduleName, $parentDoc)
	{
		$xmlObject = f_xml_Xpath::getInstance($this->getXmlFilePath($moduleName, "folderList.xml"));

		$result["configModule"] = $moduleName;
        $result["deleteRecursive"] = $xmlObject->getAttributeValue("deleteRecursive", false);
		$result["newDocumentRoute"] = $xmlObject->fromXpath("folderList")->getAttributeValue("newDocumentRoute");
		$result["docType"] = $xmlObject->fromXpath("list")->getAttributeValue("documents");

		$rootXmlObject = f_xml_Xpath::getInstance($this->getXmlFilePath($moduleName, "treeParser.xml"));

		$rootNode = $rootXmlObject->fromXpath("rootNode");

		$result["label"] = $rootNode->getAttributeValue("label");
		$result["icon"] = $rootNode->getAttributeValue("icon");

		return $result;
	}
}